//
//  photoBrowserCell.swift
//  DSWeibo
//
//  Created by J on 16/5/3.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SDWebImage.UIImageView_WebCache

protocol photoBrowserCellProtocol: NSObjectProtocol{
    
    func photoCellDidClose(cell: photoBrowserCell)
}

class photoBrowserCell: UICollectionViewCell {

    weak var photoBrowserCellDelegate : photoBrowserCellProtocol?//代理
    
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    var imageURL:NSURL?{
        didSet{
            //1.重置属性
            reset()
            //2.设置图片
            
            iconView.sd_setImageWithURL(imageURL) { (img:UIImage!, error:NSError!,_, url:NSURL!) in
                
            //3.设置图片和位置
            self.setImageViewPosition()//调整图片的尺寸和位置
            
            }
        }
    }
    
    //重置cell里面控件的属性
    private func reset(){
        
        //重置scrollView
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
        
        //重置imageView
        iconView.transform = CGAffineTransformIdentity
        
    }
    
 
    
    
    //根据图片大小调整图片显示的位置
    private func setImageViewPosition(){
        let size = self.disPlaySize(iconView.image!)//计算出来的比例尺寸
        iconView.frame = CGRect(origin: CGPointZero, size: size)

        //拿到按照宽高比计算之后的图片大小
        //2判读图片的高度 如果>屏幕高度

        //2.2短图<  设置边距让图片剧中显示
        if size.height < UIScreen.mainScreen().bounds.size.height {
            //处理剧中显示
            let y = (UIScreen.mainScreen().bounds.size.height - size.height) * 0.5
            scrollView.contentInset = UIEdgeInsets(top: y , left: 0, bottom: y , right: 0)
        }else{ //2.1  长度>屏幕高度  设置scrllView滚动范围为图片的大小

            scrollView.contentSize = size
            
            
        }

        
        
        
        
    }
    
    
    func disPlaySize(image:UIImage) -> CGSize{
        let size = image.size
        let rate = size.height / size.width//拿到图片宽高比
        return CGSize(width: self.screenWidth,height: self.screenWidth*rate)

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()//初始化UI
        
    }
    
    private func setupUI(){
      self.contentView.backgroundColor = UIColor.blackColor()
      contentView.addSubview(scrollView)
      scrollView.addSubview(iconView)

        //布局子控件
        scrollView.frame = UIScreen.mainScreen().bounds
        //处理缩放
        scrollView.delegate = self
        scrollView.maximumZoomScale = 2.0//最大尺寸
        scrollView.minimumZoomScale = 0.5//最小尺寸
    
        iconView.userInteractionEnabled = true
        //监听图片点击
        let tap = UITapGestureRecognizer(target: self, action: #selector(close))
        iconView .addGestureRecognizer(tap)
    }
    
    
    //dismiss 图片浏览器
    func close(){
        photoBrowserCellDelegate?.photoCellDidClose(self)
    }
    //懒加载
    private lazy var scrollView: UIScrollView = UIScrollView()
     lazy var iconView:   UIImageView  = UIImageView()


    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension photoBrowserCell:UIScrollViewDelegate{
    
    
    //需要缩放的视图
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return iconView
    }
    
 
    
    func scrollViewDidZoom(scrollView: UIScrollView) {

        zoomingView(iconView)

    }

    
    
    //view被缩放的view
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        zoomingView(view!)
    }
    
    private func zoomingView(view:UIView){

    //scorollView缩放的view bounds不会变 frame会变
    //缩放的本质是修改transform 所以不会影响到bounds
//    print(view.bounds)
//    print(view.frame)
    
    
    var offSetY = (screenHeight - view.frame.size.height) * 0.5
    var offSetX = (screenWidth - view.frame.size.width) * 0.5
    offSetX = offSetX < 0 ? 0 : offSetX
    offSetY = offSetY < 0 ? 0 : offSetY
    
    scrollView.contentInset = UIEdgeInsets(top: offSetY , left: offSetX, bottom: offSetY , right: offSetX)
    
    
    }
}





